<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单列表</title>
<!--    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>-->
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fa;
            padding: 20px;
        }

        .table-container {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 15px;
            max-width: 100%;
            margin: 0 auto;
            overflow-x: auto;
        }

        table {
            border-collapse: collapse;
            width: 100%;
            min-width: 1200px;
        }

        th {
            background-color: #f8fafc;
            color: #4a5568;
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.85em;
            letter-spacing: 0.5px;
            position: sticky;
            top: 0;
            white-space: nowrap;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e0e0e0;
        }

        .parent-row {
            cursor: pointer;
            background-color: #f8fafc;
            transition: all 0.2s ease;
            position: relative;
            border-left: 3px solid #4299e1;
            font-size: 0.9em;
        }

        .parent-row:hover {
            background-color: #ebf8ff;
        }

        .child-table {
            width: calc(100% - 30px);
            margin: 8px 0 16px 30px;
            border-collapse: separate;
            border-spacing: 0 6px;
        }

        .child-table thead th {
            background-color: #edf2f7;
            padding: 10px 15px;
            font-size: 0.8em;
            border-bottom: 2px solid #e2e8f0;
            position: sticky;
            top: 40px;
            white-space: nowrap;
        }

        .child-table tbody tr {
            background-color: #fff;
            transition: all 0.2s ease;
            position: relative;
        }

        .child-table tbody tr:after {
            content: "";
            position: absolute;
            left: 0;
            right: 0;
            bottom: -3px;
            height: 1px;
            background: linear-gradient(to right, transparent, #e2e8f0, transparent);
        }

        .child-table td {
            padding: 14px 15px;
            color: #4a5568;
            border: none;
            border-top: 1px solid #edf2f7;
            border-bottom: 1px solid #edf2f7;
        }

        .child-table td:first-child {
            border-left: 1px solid #edf2f7;
            border-top-left-radius: 4px;
            border-bottom-left-radius: 4px;
            padding-left: 15px; /* 确保子级第一列不缩进 */
        }

        .child-table td:last-child {
            border-right: 1px solid #edf2f7;
            border-top-right-radius: 4px;
            border-bottom-right-radius: 4px;
        }

        .child-table tr:hover td {
            background-color: #f8fafc;
            border-color: #cbd5e0;
        }

        .arrow {
            display: inline-block;
            width: 20px;
            text-align: center;
            font-size: 12px;
            color: #4299e1;
            transition: transform 0.2s ease;
        }

        .arrow::before {
            content: "▶";
        }

        .arrow.down::before {
            content: "▼";
        }

        .hidden {
            display: none;
        }

        .parent-title {
            font-weight: 600;
            color: #2d3748;
        }

        .status {
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 500;
            display: inline-block;
            min-width: 60px;
            text-align: center;
        }

        .status-ongoing {
            background-color: #ebf8ff;
            color: #3182ce;
            border: 1px solid #bee3f8;
        }

        .status-completed {
            background-color: #f0fff4;
            color: #38a169;
            border: 1px solid #c6f6d5;
        }

        .status-pending {
            background-color: #fffaf0;
            color: #dd6b20;
            border: 1px solid #feebc8;
        }

        .status-overdue {
            background-color: #fff5f5;
            color: #e53e3e;
            border: 1px solid #fed7d7;
        }

        /* 逾期状态红色文字 */
        .text-overdue {
            color: #e53e3e !important;
            font-weight: 500;
        }

        .child-container {
            padding: 0;
        }

        .child-container td {
            padding: 0;
            border: none;
        }

        /* 长文本处理 */
        .long-text {
            max-width: 200px;
            white-space: normal;
            word-break: break-word;
            line-height: 1.4;
            font-size: 0.85em;
        }

        /* 固定列宽 */
        .col-orderid {
            width: 120px;
        }

        .col-amount {
            width: 100px;
            font-size: 0.85em;
        }

        .col-fee {
            width: 100px;
            font-size: 0.85em;
        }

        .col-paid {
            width: 100px;
            font-size: 0.85em;
        }

        .col-discount {
            width: 100px;
            font-size: 0.85em;
        }

        .col-status {
            width: 100px;
            font-size: 0.85em;
        }

        .col-overdue {
            width: 100px;
            font-size: 0.85em;
        }

        .col-history {
            width: 100px;
            font-size: 0.85em;
        }

        .col-remark {
            width: 200px;
            font-size: 0.85em;
        }

        .col-time {
            width: 150px;
            font-size: 0.85em;
        }

        .col-period {
            width: 80px;
        }

        .col-duetime {
            width: 120px;
            font-size: 0.85em;
        }

        /* 优惠信息样式 */
        .discount-info {
            line-height: 1.8;
            font-size: 0.85em;
        }

        .discount-info div {
            margin-bottom: 2px;
        }

        .discount-info div:last-child {
            margin-bottom: 0;
        }
    </style>
</head>
<body>
<div class="table-container">
    <table id="main-table">
        <thead>
        <tr>
            <th class="col-orderid">订单id</th>
            <th class="col-amount">借款信息</th>
            <th class="col-paid">已还信息</th>
            <th class="col-discount">优惠信息</th>
            <th class="col-status">状态</th>
            <th class="col-overdue">当前逾期</th>
            <th class="col-history">历史逾期</th>
            <th class="col-remark">审核备注</th>
            <th class="col-remark">备注</th>
            <th class="col-time">申请时间</th>
        </tr>
        </thead>
        <tbody id="table-body">
        <!-- 动态生成表格行 -->
        </tbody>
    </table>
</div>
<script src="/jquery-3.5.1.min.js"></script>
<script src="/url.js"></script>
<script>
    let url = requestUrl + "/erupt-api/order/getList";
    let urlSearchParams = new URLSearchParams(window.location.search);
    let tokenJson = localStorage.getItem('_token');
    let jsonObj = JSON.parse(tokenJson);

    getOrderList()

    function getOrderList() {
        $.ajax({
            type: "POST",
            url: url,
            headers: {
                token: jsonObj.token
            },
            data: {userId: urlSearchParams.get('userId')},
            datatype: "json",
            success: function (result) {
                if (result.code === 1) {
                    buildTable(result.data);
                    initClickEvent();
                } else {
                    alert(result.msg)
                }
            }
        });
    }

    function buildTable(data) {
        const tbody = $("#table-body");
        tbody.empty();

        data.forEach((item, index) => {
            const parentRow = $(`
                <tr class="parent-row" data-parent-id="${index}">
                  <td class="col-orderid"><span class="arrow"></span> <span class="parent-title">${item.id}</span></td>
                  <td class="col-amount">
                    <div class="amount-info">
                      <div>金额: ${item.money}</div>
                      <div>担保费: ${item.guarantee}</div>
                      <div>利息: ${item.rateMoney}</div>
                    </div>
                  </td>
                  <td class="col-paid">
                    <div class="discount-info">
                      <div>本息: ${item.payMoney}</div>
                      <div>担保费: ${item.payMoneyByGuarantee}</div>
                    </div>
                  </td>
                  <td class="col-discount">
                    <div class="discount-info">
                      <div>本息: ${item.discountsMoney}</div>
                      <div>担保费: ${item.discountsMoneyByGuarantee}</div>
                    </div>
                  </td>
                  <td class="col-status">${getStatusClass(item.status)}</td>
                  <td class="col-overdue ${item.overdueStatus ? '' : 'text-overdue'}">${getStatusOverdue(item.overdueStatus)}</td>
                  <td class="col-history ${item.hisOverdueStatus ? '' : 'text-overdue'}">${getStatusOverdue(item.hisOverdueStatus)}</td>
                  <td class="col-remark long-text">${item.sqlEx == null ? '' : item.sqlEx}</td>
                  <td class="col-remark long-text">${item.remark == null ? '' : item.remark}</td>
                  <td class="col-time">${item.startTimeByCreate}</td>
                </tr>
                <tr class="child-container hidden" data-parent-id="${index}">
                  <td colspan="9">
                    <table class="child-table">
                      <thead>
                        <tr>
                          <th class="col-period">期数</th>
                          <th class="col-duetime">应还时间</th>
                          <th class="col-amount">应还信息</th>
                          <th class="col-paid">已还信息</th>
                          <th class="col-discount">优惠信息</th>
                          <th class="col-status">状态</th>
                          <th class="col-overdue">当前逾期</th>
                          <th class="col-remark">最后还款时间</th>
                        </tr>
                      </thead>
                      <tbody>
                        ${generateChildRows(item.loanOrderBills, index)}
                      </tbody>
                    </table>
                  </td>
                </tr>
            `);
            tbody.append(parentRow);
        });
    }

    function generateChildRows(children, parentId) {
        if (!children || children.length === 0) {
            return '<tr><td colspan="9" style="text-align:center;color:#a0aec0;padding:20px 15px;font-style:italic;">暂无还款计划</td></tr>';
        }
        return children.map(child => {
            return `
                <tr>
                  <td class="col-period">${child.period}</td>
                  <td class="col-duetime">${child.payTimeStr}</td>
                   <td class="col-amount">
                    <div class="discount-info">
                      <div>本息: ${child.money + child.rateMoney}</div>
                      <div>担保费: ${child.guarantee}</div>
                    </div>
                  </td>
                  <td class="col-paid">
                    <div class="discount-info">
                      <div>本息: ${child.payMoney}</div>
                      <div>担保费: ${child.payMoneyByGuarantee}</div>
                    </div>
                  </td>
                  <td class="col-discount">
                    <div class="discount-info">
                      <div>本息: ${child.discountsMoney}</div>
                      <div>担保费: ${child.discountsMoneyByGuarantee}</div>
                    </div>
                  </td>
                  <td class="col-status">${getPayStatus(child.payStatus)}</td>
                  <td class="col-overdue ${child.overdueStatus ? '' : 'text-overdue'}">${getStatusOverdue(child.overdueStatus)}</td>
                  <td class="col-remark long-text">${child.opTimeStr == null ? '' : child.opTimeStr}</td>
                </tr>
            `;
        }).join('');
    }

    function getStatusClass(status) {
        if (status === 1) return '等待审核';
        if (status === 2) return '审核拒绝';
        if (status === 3) return '待签署';
        if (status === 4) return '待放款';
        if (status === 5) return '放款驳回';
        if (status === 7) return '待还款';
        if (status === 10) return '已结清';
        return '未知';
    }

    function getPayStatus(status) {
        if (status) return '已结清';
        return '未结清';
    }

    function getStatusOverdue(status) {
        if (status === null) return '-';
        return status ? '正常' : '逾期';
    }

    function initClickEvent() {
        $(document).on('click', '.parent-row', function () {
            const parentId = $(this).data("parent-id");
            $(this).find(".arrow").toggleClass("down");
            $(`.child-container[data-parent-id="${parentId}"]`).toggleClass("hidden");
        });
    }
</script>
</body>
</html>